﻿using SFast.DB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SFast;
using SolutionBLL;
using SolutionModel;
using SFast.Web;

namespace MvcUI.Areas.AdminWeb.Controllers.AdminSystem
{
    public class UserController : Controller
    {
        //
        // GET: /AdminWeb/User/
        T_AdminUserBLL bll = new T_AdminUserBLL();
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Edit()
        {
            return View((object)"edit");
        }

        public ActionResult Add()
        {
            return View((object)"add");
        }

        public ActionResult DialogUserList()
        {
            return View();
        }

        public ActionResult EditSave(T_AdminUser adminusr)
        {
            try
            {
                //T_AdminUser adminusr = Request._GetForm<T_AdminUser>();
                bll.UpdateAdminUser(adminusr, Request.Form["rolesId"]);
                Response.Write("{\"info\":\"管理员编辑成功！\",\"status\":\"y\"}");
            }
            catch (Exception ex)
            {
                throw new Exception("{\"info\":\"管理员编辑失败。详细错误信息：{0}\",\"status\":\"n\"}", ex);
            }
            Response.End();
            return null;
        }

        public ActionResult AddSave()
        {
            return null;
        }

        #region getUserList 获取所有用户
        /// <summary>
        ///  获取所有用户
        /// </summary>
        /// <returns></returns>
        public JsonResult getUserList(String key)
        {
            T_AdminUserBLL bll = new T_AdminUserBLL();
            List<T_AdminUser> users = bll.getAdminUserAll(key);
            return Json(users);
        }

        /// <summary>
        /// 显示所有用户，指定角色下的用户不显示
        /// 用于角色列表页-- 用户列表（添加）弹出窗
        /// </summary>
        /// <param name="key">关键词</param>
        /// <param name="roleId">指定角色下的用户不显示</param>
        /// <returns></returns>
        public JsonResult getUserOutRole(String key, String roleId)
        {
            T_AdminUserBLL bll = new T_AdminUserBLL();
            List<T_AdminUser> users = bll.getAdminUserAll(key,roleId);
            return Json(users);
        }
        #endregion

        #region  getAdminRolesByAdminId 通过管理员ID 获取角色信息
        /// <summary>
        ///  通过管理员ID 获取角色信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public String getAdminRolesByAdminId(String id)
        {
            T_UsersInRolesBLL bl = new T_UsersInRolesBLL();
            if (!id._HasVal())
            {
                return "[]";
            }
            String jsonStr = bl.GetRolesByUser(id)._ToJsonStr();
            return jsonStr;
        }
        #endregion

        #region verifyName 验证用户是否已经存在
        /// <summary>
        /// 验证用户是否已经存在
        /// </summary>
        /// <returns></returns>
        public String verifyName()
        {
            T_AdminUserBLL bll = new T_AdminUserBLL();
            String Account = Request["param"];
            int rs = bll.HasAdminUser(Account);
            if (rs > 0)
            {
                return "对不起，此用户不可用！";
            }
            else
            {
                return "y";
            }
        }
        #endregion
    }
}
